package com.yuncheng.common.base.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuncheng.common.base.entity.BaseEntity;
import com.yuncheng.common.base.entity.CcBaseEntity;
import com.yuncheng.common.utils.CcEntityUtil;
import com.yuncheng.exception.SystemException;
import com.yuncheng.utils.I18nUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CcBaseServiceImpl<M extends BaseMapper<T>, T extends CcBaseEntity> extends ServiceImpl<M, T> {

    private static final Logger log = LoggerFactory.getLogger(CcBaseServiceImpl.class);

    /**
     * 实体单条数据保存
     * @param entity
     * @param request
     * @return
     */
    public  boolean save(T entity, HttpServletRequest request) {
         try{
             if(entity.getCreateTime()==null){
                 CcEntityUtil.setSysProperties(entity, CcEntityUtil.OpType.insert,request);
             }
         }catch (Exception e){
             log.error(e.getMessage(),e);
             throw new SystemException(I18nUtil.message("server.unknown.error"));
         }
         return  super.save(entity);
    }

    /**
     * 保存或更新
     * @param entity
     * @param request
     * @return
     */
    public  boolean saveOrUpdate(T entity, HttpServletRequest request) {
        try{
            if(entity.getCreateTime()==null){
                CcEntityUtil.setSysProperties(entity, CcEntityUtil.OpType.insert,request);
            }else{
                CcEntityUtil.setSysProperties(entity, CcEntityUtil.OpType.update,request);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new SystemException(I18nUtil.message("server.unknown.error"));
        }
        return  super.saveOrUpdate(entity);
    }

    /**
     * 列表保存
     * @param list
     * @param request
     * @return
     */
    public  boolean saveBatch(List<T> list, HttpServletRequest request) {
        try{
            list.forEach(item->{
                if(item.getCreateTime()==null){
                    CcEntityUtil.setSysProperties(item, CcEntityUtil.OpType.insert,request);
                }
            });
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new SystemException(I18nUtil.message("server.unknown.error"));
        }
        return  super.saveBatch(list);
    }

    /**
     * 单条数据更新
     * @param entity
     * @param request
     * @return
     */
    public boolean updateById(T entity,HttpServletRequest request) {
         try{
                 CcEntityUtil.setSysProperties(entity, CcEntityUtil.OpType.update,request);
         }catch (Exception e){
             log.error(e.getMessage(),e);
             throw new SystemException(I18nUtil.message("server.unknown.error"));
         }
        return super.updateById(entity);
    }

}
